package com.hisense.ovcloud.warehouse.dao;

import com.hisense.ovcloud.warehouse.Constants;
import com.hisense.ovcloud.warehouse.entity.matrix.KeyInfo;
import com.hisense.ovcloud.warehouse.entity.tableDef.KeyInfoTableDef;
import com.hisense.ovcloud.warehouse.mapper.matrix.KeyInfoMapper;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class KeyInfoDao extends ServiceImpl<KeyInfoMapper, KeyInfo> {
    private final QueryWrapper queryWrapper;

    public KeyInfoDao(QueryWrapper queryWrapper) {
        this.queryWrapper = queryWrapper;
    }


    /**
     * 查询全部
     */
    public List<KeyInfo> selectAll(){
        return this.getMapper().selectAll();
    }

    public List<KeyInfo> selectByDeveloperId(long developerId){
        return this.getMapper().selectListByQuery(queryWrapper
                .select()
                .from(KeyInfo.class)
                .where(KeyInfoTableDef.KEY_INFO.DEVELOPER_ID.eq(developerId))
        );
    }

//    public List<DomainInfo> selectDomainInfoByFeatureCode(String featureCode){
//        return this.getMapper().selectListByQueryAs(queryWrapper
//                .select(ProductInfoTableDef.PRODUCT_INFO.DOMAIN, ProductInfoTableDef.PRODUCT_INFO.SUB_DOMAIN)
//                .from(ProductInfo.class)
//                .where(ProductInfoTableDef.PRODUCT_INFO.FEATURECODE.eq(featureCode)), DomainInfo.class
//        );
//    }

    //    /**
//     * Join
//     */
////    public List<UserDto> getInfo(Integer userId){
////        QueryWrapper query = QueryWrapper.create()
////                .select(UserTableDef.USER.ALL_COLUMNS)
////                .select(SysDeptTableDef.SYS_DEPT.DEPT_NAME)
////                .from(UserTableDef.USER).as("u")
////                .leftJoin(SysDeptTableDef.SYS_DEPT).as("d").on(UserTableDef.USER.DEPT_CODE.eq(SysDeptTableDef.SYS_DEPT.DEPT_CODE))
////                .where(UserTableDef.USER.ID.eq(userId));
////        return this.getMapper().selectListByQueryAs(query,UserDto.class);
////    }
//
//    /**
//     * 新增
//     */
    public Integer insert(KeyInfo info){
        return this.getMapper().insertSelective(info);
    }

    @UseDataSource(Constants.DS.PLATFORM)
    public Integer insertByDB(KeyInfo info){
        String str = "insert into tbl_key_info(developer_id, access_key, secret_key, status, permission, description) values (333, '', '', 1, '', '')";
        return Db.insertBySql(str);
    }

    /**
     * 更新
     */
    public void update(KeyInfo info){
        this.getMapper().update(info);
    }

    @UseDataSource(Constants.DS.PLATFORM)
    public boolean updateStatus(int status, String developId, String accessKey){
        UpdateChain.of(KeyInfo.class)
                .set(KeyInfo::getStatus, status)
                .where(KeyInfoTableDef.KEY_INFO.DEVELOPER_ID.eq(developId))
                .and(KeyInfoTableDef.KEY_INFO.ACCESS_KEY.eq(accessKey))
                .update();
        return true;
    }

    @UseDataSource(Constants.DS.PLATFORM)
    public boolean updateDescription(String description, String developId, String accessKey){
        UpdateChain.of(KeyInfo.class)
                .set(KeyInfo::getDescription, description)
                .where(KeyInfoTableDef.KEY_INFO.DEVELOPER_ID.eq(developId))
                .and(KeyInfoTableDef.KEY_INFO.ACCESS_KEY.eq(accessKey))
                .update();
        return true;
    }

}
